package com.columbia.gui;

import com.columbia.CommandGenerator;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import javafx.stage.FileChooser;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.prefs.Preferences;

public class Controller {
    public static final String PO_COLUMBIA = "po-columbia";
    private static final String PO_CLIENT = "po-client";
    @FXML
    private ResourceBundle resources;

    @FXML
    private URL location;

    @FXML
    private TextField colonne_1;

    @FXML
    private TextField colonne_2;

    @FXML
    private Button go;

    @FXML
    private TextField ligne_1;

    @FXML
    private TextField ligne_2;

    @FXML
    private Button po_client_select;

    @FXML
    private TextField po_client_text;

    @FXML
    private Button po_columbia_select;

    @FXML
    private TextField po_columbia_text;

    @FXML
    private AnchorPane root;


    @FXML
    void go(ActionEvent event) {
        try {
            String po_columbia = po_columbia_text.getText();
            String po_client = po_client_text.getText();
            int ligne1 = Integer.parseInt(ligne_1.getText());
            int ligne2 = Integer.parseInt(ligne_2.getText());
            String colonne1 = colonne_1.getText();
            String colonne2 = colonne_2.getText();

            File po_columbia_file = new File(po_columbia);
            File po_client_file = new File(po_client);
            File dataFile = new File(po_client_file.getParent() + File.separator + "data.csv");
            CommandGenerator commandGenerator = new CommandGenerator( new FileInputStream(po_client_file), new FileInputStream(po_columbia_file),dataFile.getAbsolutePath(), ligne1, ligne2, colonne1, colonne2);
            commandGenerator.generationCommandesColumbia();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @FXML
    void select_client(ActionEvent event) {
        String columbiaFile = Preferences.userRoot().get(PO_CLIENT, null);
        FileChooser fileChooser = new FileChooser();
        if (columbiaFile != null) {
            File file = new File(columbiaFile);
            if (file.isDirectory()) {
                fileChooser.setInitialDirectory(file);
            } else {
                fileChooser.setInitialDirectory(new File(file.getParent()));
            }
        }

        final File file = fileChooser.showOpenDialog(root.getScene().getWindow());
        String absolutePath = file.getAbsolutePath();
        if (file != null) {
            Preferences.userRoot().put(PO_CLIENT, absolutePath);
        }

        po_client_text.setText(Preferences.userRoot().get(PO_CLIENT, null));
    }

    @FXML
    void select_columbia(ActionEvent event) {
        String columbiaFile = Preferences.userRoot().get(PO_COLUMBIA, null);
        FileChooser fileChooser = new FileChooser();
        if (columbiaFile != null) {
            File file = new File(columbiaFile);
            if (file.isDirectory()) {
                fileChooser.setInitialDirectory(file);
            } else {
                fileChooser.setInitialDirectory(new File(file.getParent()));
            }
        }

        final File file = fileChooser.showOpenDialog(root.getScene().getWindow());
        String absolutePath = file.getAbsolutePath();
        if (file != null) {

            Preferences.userRoot().put(PO_COLUMBIA, absolutePath);
        }

        po_columbia_text.setText(Preferences.userRoot().get(PO_COLUMBIA, null));
    }

    @FXML
    void initialize() {
        assert colonne_1 != null : "fx:id=\"colonne_1\" was not injected: check your FXML file 'Columbia.fxml'.";
        assert colonne_2 != null : "fx:id=\"colonne_2\" was not injected: check your FXML file 'Columbia.fxml'.";
        assert go != null : "fx:id=\"go\" was not injected: check your FXML file 'Columbia.fxml'.";
        assert ligne_1 != null : "fx:id=\"ligne_1\" was not injected: check your FXML file 'Columbia.fxml'.";
        assert ligne_2 != null : "fx:id=\"ligne_2\" was not injected: check your FXML file 'Columbia.fxml'.";
        assert po_client_select != null : "fx:id=\"po_client_select\" was not injected: check your FXML file 'Columbia.fxml'.";
        assert po_client_text != null : "fx:id=\"po_client_text\" was not injected: check your FXML file 'Columbia.fxml'.";
        assert po_columbia_select != null : "fx:id=\"po_columbia_select\" was not injected: check your FXML file 'Columbia.fxml'.";
        assert po_columbia_text != null : "fx:id=\"po_columbia_text\" was not injected: check your FXML file 'Columbia.fxml'.";
        assert root != null : "fx:id=\"root\" was not injected: check your FXML file 'Columbia.fxml'.";


        po_columbia_text.setText(Preferences.userRoot().get(PO_COLUMBIA, null));
        po_client_text.setText(Preferences.userRoot().get(PO_CLIENT, null));
    }

}
